package ClientLibrary.UI;

import ClientLibrary.Network.NetLayer;
import CommonLibrary.Commnads.AuthorizeRequest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Collections;
import java.util.Dictionary;

public class MenuForm {
    private JFrame regFrame = new JFrame("Registration");
    private Container regCont = regFrame.getContentPane();
    private NetLayer netLayer;
    public int gameIDs[];
    public String names[];
    private int sides[];
    private Frame frm;
    
    private static MenuForm currentForm;
    
    public static MenuForm getCurrentForm()
    {
        return currentForm;
    }

    public MenuForm(int gameIDs[], String names[], int sides[], NetLayer layer){
        this.gameIDs = gameIDs;
        this.names = names;
        this.sides = sides;
        this.netLayer = layer;

        currentForm = this;
    }

    public void paintFrame() {

        this.frm = new Frame("DataEntry frame");
        frm.setSize(350,500);


        String elements[] = new String[this.names.length + 2];
        for(int i = 0; i < this.names.length; i++)
        {
            elements[i] = String.format("%s - %s", this.names[i], (this.sides[i] == 0 ? "black" : "white"));
        }
        elements[this.names.length] = "New Game(White)";
        elements[this.names.length + 1] = "New Game(Black)";

        JPanel panel = new JPanel(new BorderLayout());
        JList list = new JList(elements);
        panel.add(list);
        frm.add(panel);
        for(int i = 0; i < panel.getComponentCount(); i++)
            panel.getComponents()[0].addMouseListener(new MListener(this.netLayer, this));

        frm.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        Button Submit=new Button("Login");
        Submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        RegistrationForm.getCurrentForm().close();
        frm.setVisible(true);
    }

    public void setNetLayer(NetLayer netLayer){
        this.netLayer = netLayer;
    }

    public void close(){
        this.frm.setVisible(false);
    }
    
    public int getGameId(String userName)
    {
        for(int i = 0; i < this.names.length; i++)
        {
            if(this.names[i].compareTo(userName) == 0)
            {
                return this.gameIDs[i];
            }
        }

        return -1;
    }
}
